<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="医院类型" prop="type">
        <el-select v-model="queryParams.type" @change="handleQuery" placeholder="请选择…" clearable>
          <el-option v-for="dict in dict.type.hospital_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="医院名字" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入医院名字" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="医院状态" prop="status">
        <el-select v-model="queryParams.status" @change="handleQuery" placeholder="请选择…" clearable>
          <el-option label="正常" value="0"/>
          <el-option label="停用" value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['hospital:info:add']" >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-info" size="mini" :disabled="single" @click="handleInfo" >详情</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['hospital:info:edit']" >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['hospital:info:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['hospital:info:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="医院名字" align="center" prop="hospitalName" />
      <el-table-column label="医院类型" align="center" prop="type" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hospital_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="leader" />
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="绑定时长" align="center" prop="bindDuration" >
        <template slot-scope="scope">{{scope.row.bindDuration || '--' }}小时</template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '0'" size="mini" type="success">正常</el-tag>
          <el-tag v-if="scope.row.status === '1'" size="mini" type="danger">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-info" @click="handleInfo(scope.row)" >详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['hospital:info:edit']" >修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['hospital:info:remove']" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改医院信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="医院名称" prop="hospitalName">
          <el-input v-model="form.hospitalName" placeholder="请输入医院名称" />
        </el-form-item>
        <el-form-item label="绑定时长" prop="bindDuration">
          <el-input-number v-model="form.bindDuration" placeholder="请输入绑定时长" :min="24" :max="50" :precision="0" style="width: 100%" />
        </el-form-item>
        <el-form-item label="医院类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择…" style="width: 100%;">
            <el-option
              v-for="dict in dict.type.hospital_type"
              :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="医院地址" prop="province">
          <el-cascader :options="pcaTextArr()" filterable
            v-model="codeArray" @change="handleChange" style="width: 100%" />
        </el-form-item>
        <el-form-item label="" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="form.leader" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情框 -->
    <el-dialog title="查看详情" :visible.sync="openInfo" width="500px" append-to-body>
      <el-row>
        <el-col :span="8" class="info-label">医院类型：</el-col>
        <el-col :span="16" class="info-content">
          <dict-tag :options="dict.type.hospital_type" :value="infoObj.type"/>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8" class="info-label">医院名称：</el-col>
        <el-col :span="16" class="info-content">{{ infoObj.hospitalName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="8" class="info-label">所属地区：</el-col>
        <el-col :span="16" class="info-content">{{ (infoObj.province||'未知') + '-' + (infoObj.city||'未知') + '-' + (infoObj.area||'未知') }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="8" class="info-label">详细地址：</el-col>
        <el-col :span="16" class="info-content">{{ infoObj.address }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="8" class="info-label">负责人：</el-col>
        <el-col :span="16" class="info-content">{{ infoObj.leader }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="8" class="info-label">手机号码：</el-col>
        <el-col :span="16" class="info-content">{{ infoObj.phone }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="8" class="info-label">邮箱：</el-col>
        <el-col :span="16" class="info-content">{{ infoObj.email }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="8" class="info-label">状态：</el-col>
        <el-col :span="16" class="info-content">
          <el-tag v-if="infoObj.status === '0'" size="mini" type="success">正常</el-tag>
          <el-tag v-if="infoObj.status === '1'" size="mini" type="danger">停用</el-tag>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8" class="info-label">创建人：</el-col>
        <el-col :span="16" class="info-content">{{ infoObj.createBy }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="8" class="info-label">创建时间：</el-col>
        <el-col :span="16" class="info-content">{{ infoObj.createTime }}</el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openInfo = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/business/hospital";
import { pcaTextArr } from 'element-china-area-data';

export default {
  name: "HospitalInfo",
  dicts: ['hospital_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 医院信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        status: null,
      },
      // 表单参数
      form: {status: '0'},
      // 表单校验
      rules: {
        // type: [
        //   { required: true, message: "医院类型不能为空", trigger: "blur" }
        // ],
        hospitalName: [
          { required: true, message: "医院名称不能为空", trigger: "blur" }
        ],
        phone: [
          {
            pattern: /^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        email: [
          {
            pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
      },
      codeArray: [],
      // 是否显示信息弹出层
      openInfo: false,
      infoObj: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    pcaTextArr() {
      return pcaTextArr
    },
    /** 查询医院信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hospitalId: null,
        hospitalName: null,
        bindDuration: 25,
        type: null,
        province: null,
        city: null,
        area: null,
        address: null,
        leader: null,
        phone: null,
        email: null,
        status: '0',
      };
      this.codeArray = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.hospitalId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加医院信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const hospitalId = row.hospitalId || this.ids
      getInfo(hospitalId).then(response => {
        this.form = response.data;
        this.codeArray = [this.form.province, this.form.city, this.form.area];
        this.open = true;
        this.title = "修改医院信息";
      });
    },
    /** 详情按钮操作 */
    handleInfo(row) {
      this.reset();
      const hospitalId = row.hospitalId || this.ids
      getInfo(hospitalId).then(response => {
        this.infoObj = response.data;
        this.openInfo = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.hospitalId != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hospitalIds = row.hospitalId || this.ids;
      this.$modal.confirm('该操作不可撤销，是否确认删除所选医院？').then(function() {
        return delInfo(hospitalIds);
      }).then(() => {
        this.getList();
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hospital/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    handleChange(){
      switch (this.codeArray.length) {
        case 1:
          this.form.province = this.codeArray[0];
          break;
        case 2:
          this.form.province = this.codeArray[0];
          this.form.city = this.codeArray[1];
          break;
        case 3:
          this.form.province = this.codeArray[0];
          this.form.city = this.codeArray[1];
          this.form.area = this.codeArray[2];
          break;
        default:
          this.form.province = "";
          this.form.city = "";
          this.form.area = "";
          break;
      }
    }
  }
};
</script>

<style scoped lang="scss">
.info-label {
  font-weight: bold;
  text-align: right;
  line-height: 30px;
}
.info-content {
  line-height: 30px;
}
</style>
